import { getAddress, getDiffCity, getWare } from '@/api/commonApi';

export const mapInfo = {
  methods: {
    getLocation() { // 初次获取定位
      uni.getLocation({
        // #ifdef MP-WEIXIN
        type: 'wgs84',
        // #endif
        success: (res) => {
          this.getAddress(res.latitude, res.longitude) // 经纬度获取定位信息
        },
        fail: () => {
          this.$u.toast('拒绝授权');
          const params = {
            name: '北京市',
            level: 102
          }
          this.getCityInfo(params)
        }
      });
    },
    getAddress(lat, lng) { // 获取地址信息
      const params = { lat, lng }
      getAddress(params).then((res) => {
        const { districtName, cityName } = res.data
        if (res.errorCode == 0 && !!districtName) {
          this.getDiffCity(districtName, cityName)
        } else { // 定位数据异常默认北京
          const params = {
            name: '北京市',
            level: 102
          }
          this.getCityInfo(params)
        }
      })
    },
    getDiffCity(districtName, cityName) { // 获取特殊城市
      const params = { type: 10 }
      getDiffCity(params).then((res) => {
        if (res.errorCode == 0) {
          this.diffCityList = res.data
          // 特殊城市展示三级、不是特殊城市展示二级
          var Address = res.data.includes(cityName) ? districtName : cityName
          const params = {
            name: Address,
            level: res.data.includes(cityName) ? 103 : 102
          }
          this.getCityInfo(params)
        }
      })
    },
    getCityInfo(params) { // 根据name 和 level 获取某个地区的详细数据
      getWare(params).then((res) => {
        if (res.errorCode == 0) {
          var cityParams = {}
          const { grandpa_code, grandpa_name, parent_name, parent_code, name, code, warehouse_code, level } = res.data
          var customer_warehouse_code = ''
          if (this.vuex_crmCustomerInfos && this.vuex_crmCustomerInfos.length > 0) {
            const { customerSetupInfo } = this.vuex_crmCustomerInfos[0]
            //指定仓客户，取指定仓，不合地址绑定
            if (customerSetupInfo && customerSetupInfo.warehouseCode && customerSetupInfo.warehouseCode.length > 0 && customerSetupInfo.warehouseCode != 0) {
              customer_warehouse_code = customerSetupInfo.warehouseCode
            }
          }
          if (level == 102) {
            cityParams = {
              provinceCode: parent_code,
              provinceName: parent_name,
              cityCode: code,
              cityName: name,
              areaCode: '',
              areaName: '',
              warehouseCode: customer_warehouse_code || warehouse_code
            }
          } else if (level == 103) {
            cityParams = {
              provinceCode: grandpa_code,
              provinceName: grandpa_name,
              cityCode: parent_code,
              cityName: parent_name,
              areaCode: code,
              areaName: name,
              warehouseCode: customer_warehouse_code || warehouse_code
            }
          }
          console.log()
          uni.$u.vuex('vuex_cityInfo', cityParams);
        }
      })
    }
  }
}
